package com.gtq.uhome.controller;

import com.gtq.uhome.pojo.Role;
import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/role")
@CrossOrigin
@Api(tags = "系统管理——角色管理——何凡")
public class RoleController {

    @Resource
    private RoleService roleService;//实例化

    /**
     * 查询所有角色信息及创建时间
     * @return Result<List<Role>>
     */
    @GetMapping
    @ResponseBody
    @ApiOperation(value = "查询所有角色信息及创建时间")
    public Result<List<Role>>selectByRolesAll(){
        return Result.ok(roleService.selectRoleAll());
    }

    /**
     * 根据条件查询角色信息及创建时间
     * @param roleName
     * @return Result<List<Role>>
     */
    @PostMapping(value = "/roles")
    @ResponseBody
    @ApiOperation(value = "根据条件查询角色信息及创建时间")
    public Result<List<Role>>selectByRoles(@RequestBody Role roleName){
        return Result.ok(roleService.selectRole(roleName));
    }

    /**
     * 添加角色信息
     * @param role
     * @return Result 添加后的的结果集
     */
    @PostMapping(value = "/insert")
    @ResponseBody
    @ApiOperation(value = "添加角色信息")
    public Result insertRole(@RequestBody Role role) {
        System.out.println("insert开始执行"+role);
        Boolean aBoolean = roleService.insertRole(role);
        if (aBoolean) {
            return Result.ok("添加成功");
        }
        return Result.failed("添加失败，请检查角色信息是否正确");
    }

    /**
         * 修改角色信息
     * @param role
     * @return Result 修改后的结果集
     */
    @PutMapping(value ="/updateRole")
    @ResponseBody
    @ApiOperation(value = "修改角色信息")
    public Result updateRole(@RequestBody List<Role> role) {
        Boolean aBoolean = roleService.updateRole(role);
        if (aBoolean) {
            return Result.ok("修改成功");
        }
        return Result.failed("修改失败");
    }

    /**
     * 删除角色信息
     * @param role
     * @return
     */
    @PutMapping(value ="/deleteRole")
    @ResponseBody
    @ApiOperation(value = "删除角色信息")
    public Result deleteRole(@RequestBody List<Role> role) {
        Boolean aBoolean = roleService.updateRole(role);
        if (aBoolean) {
            return Result.ok("删除成功");
        }
        return Result.failed("删除失败");
    }

}

